package ed.hbut.supermarket1.controller;

import ed.hbut.supermarket1.entity.Merchandise;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.service.MerchandiseService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static java.rmi.server.LogStream.log;

@Slf4j
@RestController
public class MerchandiseController {



    @Autowired
    public MerchandiseService merchandiseService;

    /*
    新增商品
     */
    @PostMapping("/api/merchandise")
    public Result postMerchandise(@RequestBody Merchandise merchandise) {

        System.out.println("Merchandise " + merchandise);
        merchandiseService.postmechandise(merchandise);

        MerchandiseController.log.info("新增了一个商品");
        return Result.success();
    }

    /*
    查询所有商品
     */
    @GetMapping("/api/merchandise")
    public Result getAllMerchandise() {
        System.out.println("获取请求");
        List<Merchandise> merchandiseList = merchandiseService.getAllMerchandise();

        MerchandiseController.log.info("查询所有商品");
        return Result.success(merchandiseList);
    }

    /*
    根据id查询商品
     */
    @GetMapping("/api/merchandise/{id}")
    public Result getMerchandiseById(@PathVariable Integer id) {

        MerchandiseController.log.info("getMerchandiseById");
        Merchandise merchandise = merchandiseService.getById(id);
        System.out.println("merchandise:" + merchandise);

        return Result.success(merchandise);
    }

    /**
     *
     * 删除商品
     */
    @DeleteMapping("/api/merchandise/{id}")
    public Result deleteMerchandise(@PathVariable Integer id) {
        MerchandiseController.log.info("deleteMerchandise");
        merchandiseService.deleteById(id);

        MerchandiseController.log.info("删除了一个商品");
        return Result.success();
    }

    /**
     *
     * 修改商品信息
     * @param merchandise
     * @return
     */
    @PutMapping("/api/merchandise")
    public Result updateMerchandise(@RequestBody Merchandise merchandise) {
        MerchandiseController.log.info(merchandise.toString());
        merchandiseService.update(merchandise);

        MerchandiseController.log.info("修改了一个商品");
        return Result.success();
    }

}
